﻿using System;
using System.Web.Mvc;
using IterationLife.Extensions.ControllerBase;
using IterationLife.FootballTeaching.Queries;
using IterationLife.PlatformBackEnd.AppServices.Contracts.FootballTeachings;
using IterationLife.PlatformBackEnd.AppServices.Dtos.FootballTeachings;
using Util;
using Util.Webs.Mvc;

namespace IterationLife.PlatformBackEnd.WebSite.Areas.FootballTeachings.Controllers
{
    /// <summary>
    /// 课时控制器
    /// </summary>
    public class LessonController : FormControllerBase<LessonDto, LessonQuery>
    {
        /// <summary>
        /// 初始化课时控制器
        /// </summary>
        /// <param name="service">课时服务</param>
        /// <param name="chapterService">章节服务</param>
        public LessonController(ILessonService service, IChapterService chapterService)
            : base(service)
        {
            LessonService = service;
            ChapterService = chapterService;
        }

        /// <summary>
        /// 课时服务
        /// </summary>
        public ILessonService LessonService { get; private set; }

        /// <summary>
        /// 章节服务
        /// </summary>
        protected IChapterService ChapterService { get; set; }

        /// <summary>
        /// 表格
        /// </summary>
        /// <param name="chapterId">章节编号</param>
        public ActionResult Grid(Guid? chapterId)
        {
            if (chapterId.IsEmpty())
                return null;
            return PartialView("Index", new LessonDto
            {
                ChapterId = chapterId,
                ChapterName = ChapterService.GetById(chapterId)?.Name
            });
        }

        /// <summary>
        /// 新传输对象
        /// </summary>
        /// <param name="chapterId">章节编号</param>
        public ActionResult NewDto(Guid chapterId)
        {
            return PartialView("Parts/Form", LessonService.NewDto(chapterId));
        }

        /// <summary>
        /// 表单面板
        /// </summary>
        public ActionResult FormPanel(string id, int sortId)
        {
            var dto = LessonService.GetById(id);
            dto.SortId = sortId;
            return PartialView("Parts/FormPanel", dto);
        }

        [HttpPost]
        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        [FormExceptionHandler]
        [AjaxOnly]
        public override ActionResult Save(LessonDto dto)
        {
            return base.Save(dto);
        }

        /// <summary>
        /// 弹出层首页
        /// </summary>
        public ActionResult DialogIndex(LessonDto dto)
        {
            return PartialView("Dialogs/Index", dto);
        }
    }
}